﻿namespace Huayou.Utils
{
    using Microsoft.Win32;
    using System;
    using System.Text.RegularExpressions;

    public class RegConfigManager : CommonConfManager
    {
        private string strRegItemName = null;

        public RegConfigManager(string strRegName)
        {
            this.strRegItemName = strRegName;
        }

        public string GetValue(string strKeyName)
        {
            RegistryKey key = Registry.LocalMachine.OpenSubKey(this.strRegItemName, false);
            if (key != null)
            {
                object obj2 = key.GetValue(strKeyName);
                if (obj2 != null)
                {
                    return Convert.ToString(obj2);
                }
            }
            return null;
        }

        public void SetValue(string strKeyName, string strValue)
        {
            RegistryKey key = null;
            RegistryKey key2 = null;
            string[] strArray = Regex.Split(this.strRegItemName, @"\\");
            for (int i = 0; i < strArray.Length; i++)
            {
                if (i == 0)
                {
                    key = Registry.LocalMachine.OpenSubKey(strArray[i], true);
                    if (key == null)
                    {
                        key = Registry.LocalMachine.CreateSubKey(strArray[i]);
                    }
                }
                else
                {
                    key2 = key;
                    key = key.OpenSubKey(strArray[i], true);
                    if (key == null)
                    {
                        key = key2.CreateSubKey(strArray[i]);
                    }
                }
            }
            key.SetValue(strKeyName, strValue);
        }
    }
}

